package leo.game.ui
{
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.geom.ColorTransform;
	
	import ghostcat.util.easing.TweenUtil;
	
	import leo.data.GameModel;
	import leo.manager.McMannager;

	/**
	 * 关卡界面 
	 * @author leo
	 * 
	 */	
	public class UILevel extends Sprite
	{
		
		private static var _instance :UILevel = null;
		
		public var item0 :MovieClip;
		public var item1 :MovieClip;
		public var item2 :MovieClip;
		public function UILevel()
		{
			super();
			
			var mc :MovieClip = McMannager.instance.getMCByName("UI_Level");
			this.addChild(mc);
			this.item0 = mc.item0;
			this.item1 = mc.item1;
			this.item2 = mc.item2;
			this.item0.lock0.visible = false;
			
			this.item0.addEventListener(MouseEvent.CLICK,clickHandler1);
			this.item1.addEventListener(MouseEvent.CLICK,clickHandler2);
			this.item2.addEventListener(MouseEvent.CLICK,clickHandler3);
			this.addEventListener(MouseEvent.MOUSE_MOVE,moveHandler);
		}
		
		private function clickHandler1(event :MouseEvent):void
		{
			if(this.item0.lock0.visible) return;
			
			UIMap.instance.init(0);
			UITop.instance.updateLevel(0);
			this.showUILevel(1000,-250,250);
			UIBottom.instance.showBottom(1000,0,500);
			UITop.instance.showUITop(1000,0,0);
			GameModel.instance.isOver = false;
			clearUILevelColorTransform();
		}
		private function clickHandler2(event :MouseEvent):void
		{
			trace("level2");
			if(this.item1.lock1.visible) return;
			
			UIMap.instance.init(1);
			UITop.instance.updateLevel(1);
			this.showUILevel(1000,-250,250);
			UIBottom.instance.showBottom(1000,0,500);
			UITop.instance.showUITop(1000,0,0);
			GameModel.instance.isOver = false;
			clearUILevelColorTransform();
		}
		
		private function clickHandler3(event :MouseEvent):void
		{
			trace("level3");
			if(this.item2.lock2.visible) return;
			
			UIMap.instance.init(2);
			UITop.instance.updateLevel(2);
			this.showUILevel(1000,-250,250);
			UIBottom.instance.showBottom(1000,0,500);
			UITop.instance.showUITop(1000,0,0);
			GameModel.instance.isOver = false;
			clearUILevelColorTransform();
		}

		public function changeLevel(value :int):void
		{
			switch(value)
			{
				case 0:
					this.item0.lock0.visible = false;
					this.item1.lock1.visible = true;
					this.item2.lock2.visible = true;
					break;
				case 1:
					this.item0.lock0.visible = false;
					this.item1.lock1.visible = false;
					this.item2.lock2.visible = true;
					break;
				case 2:
					this.item0.lock0.visible = false;
					this.item1.lock1.visible = false;
					this.item2.lock2.visible = false;
					break;
			}
		}
		/**
		 * 显示缓动 
		 * @param duration 时间
		 * @param tx
		 * @param ty
		 * 
		 */		
		public function showUILevel(duration :int ,tx :int,ty :int):void
		{
			clearUILevelColorTransform();
			TweenUtil.to(this,duration,{x :tx,y:ty});
		}

		public static function get instance():UILevel
		{
			if(_instance == null)
			{
				_instance = new UILevel();
			}
			return _instance;
		}
		/**
		 * 清除ui上的颜色滤镜 
		 * 
		 */		
		private function clearUILevelColorTransform():void
		{
			this.item0.transform.colorTransform = new ColorTransform();
			this.item1.transform.colorTransform = new ColorTransform();
			this.item2.transform.colorTransform = new ColorTransform();
		}
		/**
		 * 移动鼠标时检测是否需要变换颜色 
		 * @param event
		 * 
		 */		
		private function moveHandler(event :MouseEvent):void
		{
			if(this.item0.hitTestPoint(stage.mouseX,stage.mouseY))
			{
				if(!this.item0.lock0.visible)
				{
					this.item0.transform.colorTransform = new ColorTransform(0,1,0);
				}else
				{
					this.item0.transform.colorTransform = new ColorTransform(0.4,0.3,0.4,1,30,40,30,0);
				}
			}else
			{
				this.item0.transform.colorTransform = new ColorTransform();
			}
			
			if(this.item1.hitTestPoint(stage.mouseX,stage.mouseY))
			{
				if(!this.item1.lock1.visible)
				{
					this.item1.transform.colorTransform = new ColorTransform(0,1,0);
				}else
				{
					this.item1.transform.colorTransform = new ColorTransform(0.4,0.3,0.4,1,30,40,30,0);
				}
			}else
			{
				this.item1.transform.colorTransform = new ColorTransform();
			}
			
			if(this.item2.hitTestPoint(stage.mouseX,stage.mouseY))
			{
				if(!this.item2.lock2.visible)
				{
					this.item2.transform.colorTransform = new ColorTransform(0,1,0);
				}else
				{
					this.item2.transform.colorTransform = new ColorTransform(0.4,0.3,0.4,1,30,40,30,0);
				}
			}else
			{
				this.item2.transform.colorTransform = new ColorTransform();
			}
		}

	}
}